from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.api import users
from google.appengine.ext import db
from models import *
from user import *

class ThingHandler(webapp.RequestHandler):
  def get(self, thingid):
    # Get the user from the db
    user = UserUtil.getUser()
    
    thingQuery = Thing.gql('WHERE thingid = :1 LIMIT 1', thingid)
    # Check if thing is in datastore
    if (thingQuery.count() == 0):
      self.response.out.write('Thing is not in db: ' + thingid + '<br />')
    else:
      # If is is already in show the id and name (just for debug purposes)
      thing = thingQuery.get()
      self.response.out.write('Thing is in db: ' + thingid + '<br />')
      
      # Check if the thing is assigned to an item
      if (thing.assigned == False):
        # Thing is not assigned
        self.response.out.write('Thing has not been assigned to an object yet<br />')
        # TODO: Allow the user to set the thing
      else:
        # Thing is assigned
        self.response.out.write('Thing has been assigned to an object: ' + thing.title + '<br />')
        # TODO: Display some information about the thing
        # TODO: Allow user to add annotations to the thing
        

def main():
  application = webapp.WSGIApplication([
    ('/ndm/thing/([^/]+)$', ThingHandler),
  ], debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
    main()
